package com.batata.continuing.service.config;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.batata.continuing.config.changeable.OpenOrClose;
import com.batata.continuing.dao.config.ConfigSwitchDao;
import com.batata.continuing.pojo.base.ResultData;
import com.batata.continuing.pojo.config.ConfigSwitch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.batata.continuing.service.common.ResultCommon.resultDataCommon;


@Service
public class ConfigSwitchService extends ServiceImpl<ConfigSwitchDao, ConfigSwitch> {

    @Autowired
    private OpenOrClose openOrClose;


    public boolean isVerify(String id) {
        QueryWrapper<ConfigSwitch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return this.baseMapper.selectOne(queryWrapper).getOpenOrClose() != 0;
    }


    /**
     * 更新开关的配置
     *
     * @return
     */
    public ResultData<Object> updateSwitchConfig() {
        openOrClose.update();
        return resultDataCommon(200, "配置已更新", true);
    }

    /**
     * 修改开关的配置
     *
     * @return
     */
    public ResultData<Object> alterSwitchConfig(ConfigSwitch configSwitch) {
        if (
                !(configSwitch.getOpenOrClose() == 0) && !(configSwitch.getOpenOrClose() == 1)
        ) {
            return resultDataCommon(500, "参数不合法", false);
        }

        if (this.baseMapper.updateById(configSwitch) == 1) {
            return resultDataCommon(200, "修改成功", true);
        } else {
            return resultDataCommon(500, "修改失败", false);
        }

    }
}
